<?php
	session_start();
	require_once('functions.php');
	
	if( $_SESSION['account_type_code'] != 1 && $_SESSION['account_type_code'] != 2) 
	{
		header("Location:index.php?response_msg=Please login.");
		exit;
	}
	
	$boatname = $_REQUEST['boat_name'];
	$boatcolor = $_REQUEST['boat_color'];
	$action = $_REQUEST['action'];
	
	if(!$boatname && !$boatcolor && $action=="search")
	{	
		$error_msg =  "Please fill in some search criteria.";
		$_REQUEST['response_msg'] = $error_msg;
	}

	if(!$error_msg && $action=="search")
	{
		$search_results = search_boat($boatname, $boatcolor); 
		
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SAILOR</title>
</head>

<body>
	<a href="user.php">Go Back to user home</a>
	<hr/>
	
	<h2 style="color:red">
		<?php
			if($_REQUEST['response_msg']) 
			{
				echo htmlspecialchars($_REQUEST['response_msg']);
				$_REQUEST['response_msg'] = '';
			}
		?>
	</h2>
	
	<h2>Search for a boat:</h2>
	
	<form method="post" action="search_boat.php">
		<label for="boat_name">Boat Name: </label>
		<input type="text" name="boat_name" value=""><br/>
		
		<label for="boat_color">Boat Color: </label>
		<input type="text" name="boat_color" value=""><br/>
		
		<input type="hidden" name="action" value="search">
		
		<input type="reset" name="" value="Reset">
		<input type="submit" name="" value="Search">
	</form>
	
	<hr/>
	
	<?php
		if(!$error_msg)
		{
	?>	
			Search results for <br/>
			<?php if($boatname){ ?>	 '<?php echo htmlspecialchars($boatname); ?>' in boat name <br/> <?php } ?>
			<?php if($boatcolor){ ?> '<?php echo htmlspecialchars($boatcolor); ?>' in color <br/> <?php } ?>
	<?php
		}
	?>
	
	<?php
		if( $search_results )
		{
	?>
				<input type="text" name="boat_id" value="Boat ID" disabled="disabled">
				<input type="text" name="boat_name" value="Name" disabled="disabled">
				<input type="text" name="boat_color" value="Color" disabled="disabled"><br/>
	<?php
		
			foreach($search_results as $result)
			{
			?>	
				<input type="text" name="boat_id" value="<?php echo $result['bid'] ?>" disabled="disabled">
				<input type="text" name="boat_name" value="<?php echo $result['bname'] ?>" disabled="disabled">
				<input type="text" name="boat_color" value="<?php echo $result['color'] ?>" disabled="disabled"><br/>
			<?php
			}
		}
		else if(!$error_msg)
		{
	?>
			<b>No rows returned.</b>
	<?php
		}
	?>
	
</body>
</html>